package com.anlgin.anl_tea.tools;

import com.anlgin.anl_tea.entities.Commodity;
import com.anlgin.anl_tea.entities.CommodityType;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class CommodityTools {

    /**
     *   格式化商品数据，便于前端遍历
     */

    public LinkedHashMap formatCommodity(List<CommodityType> types, List<Commodity> commodities) {
        LinkedHashMap<String, List<Commodity>> map = new LinkedHashMap<String, List<Commodity>>();
        List<Commodity> list = new ArrayList<Commodity>();
        for (CommodityType type: types) {
            list = new ArrayList<Commodity>();
            for (Commodity commodity : commodities) {
                if (commodity.getTypeId() == type.getTypeId()) {
                    list.add(commodity);
                }
            }
            map.put(type.getTypeName(),list);
        }
        return map;
    }

    public String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public String getCurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");
        return simpleDateFormat.format(new Date());
    }
}
